package com.blb.springboot.controller;

import com.blb.springboot.entity.User;
import com.blb.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * @author lvtian
 * @date 2023/1/10 16:29
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;

    //用于访问login页面
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "loginMenu";
    }

    //登录方法
    @RequestMapping("login")
    public String login(String username , String password , HttpSession session, Model model){
        User user = userService.login(username, password);
        if(user == null){
            //验证失败，添加提示信息
            model.addAttribute("msg","账号或密码错误");
            //请求转发，跳转登录页面
            return "forward:/toLogin";
        }
        session.setAttribute("user",user);
        return "redirect:/getAll";
    }


}
